﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Icoders.MarketTracker.Core.MasterData;
using Icoders.MarketTracker.Core.Repository.MasterDataRepositories;
using Icoders.MarketTracker.Core.Validation;
using Icoders.MarketTracker.Web.Lib.Security;
using Icoders.MarketTracker.Web.Lib.Validation;

namespace Icoders.MarketTracker.Web.Areas.Admin.Controllers
{
     [CustomAuthorize(Roles = "ROLE_ADMIN")]
    public class ProductBrandController : Controller
     {
         private IProductBrandRepository _productBrandRepository;

         public ProductBrandController(IProductBrandRepository productBrandRepository)
         {
             _productBrandRepository = productBrandRepository;
         }

         public ActionResult Index()
        {
            var item = _productBrandRepository.GetAll();
            return View(item);
        }
         public ActionResult Create()
         {
             
             return View(new ProductBrand());
         }
         [HttpPost]
         public ActionResult Create(ProductBrand model)
         {

             try
             {
                 model.Code = "123";
                 _productBrandRepository.Save(model);
                 //Saveaudit("Added Product", HttpContext.User.Identity.Name, ActionType.Added);

             }
             catch (DomainValidationException dve)
             {
                 
                 ValidationSummary.DisplayDomainValidationResult(dve, ModelState);
                 return View(model);
             }
             catch (Exception ex)
             {
                
                 ValidationSummary.DisplayValidationResult(ex.Message, ModelState);
                 return View(model);
             }
             return RedirectToAction("Index");
         }
         public ActionResult Edit(int id)
         {
            
             var model = _productBrandRepository.GetById(id);
             return View(model);
         }
         [HttpPost]
         public ActionResult Edit(ProductBrand model)
         {
             try
             {
                 _productBrandRepository.Save(model);
                 //Saveaudit("Edited Product", HttpContext.User.Identity.Name, ActionType.Updated);
             }
             catch (DomainValidationException dve)
             {
                 //Bind();
                 ValidationSummary.DisplayDomainValidationResult(dve, ModelState);
                 return View(model);
             }
             catch (Exception ex)
             {
                 //Bind();
                 ValidationSummary.DisplayValidationResult(ex.Message, ModelState);
                 return View(model);
             }
             return RedirectToAction("Index");
         }

    }
}
